#import "TransactionViewController.h"
#import "IDominoAppDelegate.h"
#import "URLLoadingOperation.h"

@implementation TransactionViewController
@synthesize transactionArray;
@synthesize historyTransactionArray;
@synthesize historyTransactionTimeArray;

- (id)initWithStyle:(UITableViewStyle)style {
	if (self = [super initWithStyle:style]) {
        self.tableView.delegate = self;
        self.tableView.dataSource = self;
        self.tableView.rowHeight = 60;
        self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
		self.hidesBottomBarWhenPushed=NO;
		transactionArray=[[NSMutableArray alloc] init];
		historyTransactionArray=[[NSMutableArray alloc] init];
		historyTransactionTimeArray=[[NSMutableArray alloc] init];		
		NSArray *segmentTextContent = [NSArray arrayWithObjects:
									   NSLocalizedString(@"RunningTransaction", @"Running Transaction Title"),
									   NSLocalizedString(@"DoneTransaction", @"Done Transaction Title"),
									   nil];
		UISegmentedControl* segmentedControl = [[UISegmentedControl alloc] initWithItems:segmentTextContent];
		segmentedControl.selectedSegmentIndex = 0;
		segmentedControl.autoresizingMask = UIViewAutoresizingFlexibleWidth;
		segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
		segmentedControl.frame = CGRectMake(0, 0, 400, 30);
		[segmentedControl addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
		
		self.navigationItem.titleView = segmentedControl;
		[segmentedControl release];
			self.tabBarItem.image=[UIImage imageNamed:@"TabTaskView.png"];
		self.tabBarItem.title=NSLocalizedString(@"Transaction", @"");
		self.hidesBottomBarWhenPushed=NO;
		isCurrent=YES;
	}
	return self;
}



#pragma mark Table view methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	if(isCurrent)
		return [transactionArray count];
	else
		return [historyTransactionArray count];
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell;
    // Set up the cell...
	if( isCurrent ){
		cell = [tableView dequeueReusableCellWithIdentifier:@"TransactionCell"];
		if(cell==nil){
			cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"TransactionCell"] autorelease];
		}
		if([transactionArray count]!=0)
			cell.textLabel.text=[transactionArray objectAtIndex:indexPath.row];
	}
	else{
		cell = [tableView dequeueReusableCellWithIdentifier:@"HistroyTransactionCell"];
		if(cell==nil){
			cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"HistroyTransactionCell"] autorelease];
		}
		if([historyTransactionArray count]!=0){
			cell.detailTextLabel.text=[historyTransactionTimeArray objectAtIndex:indexPath.row];
			cell.textLabel.text=[historyTransactionArray objectAtIndex:indexPath.row];
		}
	}
	cell.textLabel.font=[UIFont systemFontOfSize:[UIFont labelFontSize]];
	cell.selectionStyle=UITableViewCellSelectionStyleNone;
	return cell;

}


- (void)segmentAction:(id)sender
{
	UISegmentedControl* segCtl = sender;
	// the segmented control was clicked, handle it here 
	int selection=[segCtl selectedSegmentIndex];
	if(selection==0)
		isCurrent=YES;
	else if(selection==1)
		isCurrent=NO;
	[self.tableView reloadData];
}

- (void)addToTransaction:(NSString*)description
{
	[transactionArray addObject:description];
	[self.tableView reloadData];
}

- (void)addToHistroyTransaction:(NSString*)description
{
	[historyTransactionArray insertObject:description atIndex:0];
	NSDateFormatter *outputFormatter=[[NSDateFormatter alloc] init];
	[outputFormatter setDateFormat:@"H:mm a"]; 
	[historyTransactionTimeArray insertObject: [outputFormatter stringFromDate:[NSDate date]] atIndex:0];
	[outputFormatter release];	
	[self.tableView reloadData];
}

- (void)removeFromTransaction:(NSString*)description
{
	[transactionArray removeObject:description];
	[self.tableView reloadData];
}

- (void)shiftToHistoryTransaction:(NSString*)description
{
	[transactionArray removeObject:description];
	[historyTransactionArray insertObject:description atIndex:0];
	NSDateFormatter *outputFormatter=[[NSDateFormatter alloc] init];
	[outputFormatter setDateFormat:@"H:mm a"]; 
	[historyTransactionTimeArray insertObject: [outputFormatter stringFromDate:[NSDate date]] atIndex:0];
	[outputFormatter release];
}

- (void)dealloc {
	[transactionArray release];
	[historyTransactionArray release];
	[historyTransactionTimeArray release];
	[super dealloc];
}




@end

